var util = require('../../libs/util.js');
//获取应用实例
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    pageSize: 20,
    pageNumber: 0,
    count: 0,
    page:1,
    firstPage:true,
    prevPage: true,
    nextPage: true,
    lastPage: true,
    carrier:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    if (e.carrier){
      this.setData({
        carrier: e.carrier
      })
    }
    this.setData({
      CO_ID:app.globalData.CO_ID,
      role:app.globalData.role_default
    })
    this.loadData();
  },
  bindInput: function (e) {
    var item = String(e.currentTarget.id), item_value = {};
    item_value[item] = e.detail.value;
    this.setData(item_value);
  },
  search:function(){
    this.loadData();
  },
  loadData:function(){
    var that = this;
    var url = "/atapi/warehouse/warehouse";
    if (that.data.carrier){
      url = "/atapi/warehouse/warehouse/warehouseOfCarrier";
    }
    let condition = ''
    if(that.data.searchVal){
      condition = "and ( name like '%"+that.data.searchVal+"%')"
    }
    util.postData("/zhc/datalist/getDataByListId", {
      "list_id": "C1FFD7BD-140A-4842-A62C-86A330B9DDCF",
      "page": {
        "size": that.data.pageSize,
        "current": that.data.page,
        "orders": [
          {
            "column": "a.ADD_TIME",
            "asc": "false"
          }
        ]
      },
      "data": {
        "field": ""
      },
      "repeat_field": [],
      "condition": " a.CO_ID='"+app.globalData.CO_ID+"' and  a.BE_DEL=0  "+condition,
      "virtual_field": []
    }, function (res, error) {
      console.log(res); console.log(error)
      if (res.data.success && res.data?.result?.records.length > 0) {
        var count = res.data.result.total;
        var pageNumber = Math.ceil(count / that.data.pageSize);
        that.setData({
          dataList: res.data.result.records,
          count: count,
          pageNumber: pageNumber,
          page: that.data.page == 0 ? 1 : that.data.page
        });
        that.setPageStatus();
      } else {
        that.setData({
          page: 0,
          dataList: []
        });
      }
    });
  },
  deleteWarehouse:function(e){
    var that = this;
    wx.showModal({
      title: '温馨提示',
      content: '确定删除吗？',
      success(res) {
        if (res.confirm) {
          var id = e.currentTarget.dataset.id;
          util.postData("/atapi/warehouse/warehouse/deleteWarehouse", {
            'id': id
          }, function (res, error) {
            console.log(res); console.log(error)
            if (res.data.success) {
              wx.showToast({
                title: "删除成功",
                icon: "success",
                duration: 3000,
                success: function () {
                  setTimeout(function () {
                    that.loadData();
                  }, 3000);
                }
              });
            } else {
              wx.showToast({
                title: "删除失败",
                image: '/images/wrong.png',
                duration: 3000
              });
            }
          });
        }

      }
    })
  },
  setPriceConfig:function(e){
    wx.redirectTo({
      url: '/pages/enterprise/price_config?type=warehouse&id=' + e.currentTarget.dataset.id
    })
  },
  //翻页按钮状态
  setPageStatus:function(){
    var that = this;
    var page = that.data.page, pageNumber = that.data.pageNumber;
    if (pageNumber == 1){
      that.setData({
        firstPage: true,
        prevPage: true,
        nextPage: true,
        lastPage: true
      });
    } else if (pageNumber > 1){
      if (page == 1) {
        that.setData({
          firstPage: true,
          prevPage: true,
          nextPage: false,
          lastPage: false
        });
      } else if (page == pageNumber) {
        that.setData({
          firstPage: false,
          prevPage: false,
          nextPage: true,
          lastPage: true
        });
      } else if (page < pageNumber && page > 1) {
        that.setData({
          firstPage: false,
          prevPage: false,
          nextPage: false,
          lastPage: false
        });
      }
    }
  },
  //分页按钮操作
  pageChange:function(e){
     var type = e.currentTarget.dataset.page;
     if(type == 'first'){
       this.setData({
         page:1
       })
       this.loadData();
     }
    if (type == 'last') {
      this.setData({
        page: this.data.pageNumber
      })
      this.loadData();
    }
    if (type == 'prev') {
      this.setData({
        page: this.data.page - 1
      })
      this.loadData();
    }
    if (type == 'next') {
      this.setData({
        page: this.data.page + 1
      })
      this.loadData();
    }
  },
  newWarehouse: function (e) {
    wx.navigateTo({
      url: '/pages/enterprise/warehouse_new'
    })
  },
  editWarehouse:function(e){
     wx.navigateTo({
       url: '/pages/enterprise/warehouse_edit?id='+e.currentTarget.dataset.id
     })
  },
  importWarehouse: function (e) {
    wx.redirectTo({
      url: '/pages/enterprise/warehouse_import'
    })
  },
  openLocation:function(e){
    var coord = String(e.currentTarget.dataset.coord).split(",");
    wx.openLocation({
      latitude: Number(coord[0]),
      longitude: Number(coord[1]),
      scale: 18
    })
  },
  // ListTouch触摸开始
  ListTouchStart(e) {
    this.setData({
      ListTouchStart: e.touches[0].pageX
    })
  },

  // ListTouch计算方向
  ListTouchMove(e) {
    this.setData({
      ListTouchDirection: e.touches[0].pageX - this.data.ListTouchStart > 0 ? 'right' : 'left'
    })
  },

  // ListTouch计算滚动
  ListTouchEnd(e) {
    if (this.data.ListTouchDirection == 'left') {
      this.setData({
        modalName: e.currentTarget.dataset.target
      })
    } else {
      this.setData({
        modalName: null
      })
    }
    this.setData({
      ListTouchDirection: null
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})